home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 419_01 / odmg10 / lib / odmg / Array.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-28  |  3.8 KB  |  191 lines

  1. /*******************************<+>***************************
  2.  **                             DTA
  3.  *************************************************************
  4.  **
  5.  **  $Id: Array.h,v 1.8 1994/03/01 23:01:19 dta Exp $
  6.  **
  7.  **  $Source: /cvs/lib/odmg/Array.h,v $
  8.  **
  9.  **  What @(#):
  10.  **
  11.  **  Author: Dale T. Anderson
  12.  **
  13.  *******************************<+>***************************/
  14.  
  15. #ifndef _Array_h
  16. #define _Array_h
  17.  
  18. #include <Odmg/Collection.h>
  19.  
  20. template <class T> class Varray;
  21.  
  22. template <class T>
  23. class Varray :
  24.     public Collection <T>
  25. {
  26.     Position m_total;        // total number of elements allocated
  27.     int m_chunk;            // number of elements to expand by
  28.  
  29.     Ref <T> *m_array;
  30.  
  31.     virtual Ref <Collection <T> > create (void) const;
  32.  
  33.     Varray (const Ref <Varray <T> >);
  34.  
  35.     public:
  36.  
  37.     Varray (Position length, const int chunk = 10);
  38.     ~Varray ();
  39.  
  40.     Ref <T> operator [] (const Position);
  41.     void remove_element_at (const Position);
  42.     void replace_element_at (Ref <T>, const Position);
  43.     virtual Ref <T> retrieve_element_at (const Position);
  44.     void resize (Position length);
  45.     int find_element (const Ref <T>) const;
  46.     int upper_bound (void) const;
  47.  
  48.     //
  49.     // Collection methods
  50.     //
  51.  
  52.     virtual void insert_element (Ref <T>);
  53.     virtual void remove_element (Ref <T>);
  54.  
  55.     virtual void replace_element_at (Ref <T>, const Iterator <T> &);
  56. };
  57.  
  58. template <class T>
  59. Varray<T>::Varray (Position length, const int chunk) :
  60.     m_total (length),
  61.     m_chunk (chunk)
  62. {
  63.     if (length)
  64.     m_array = new Ref <T> [length];
  65.     else
  66.     m_array = NULL;
  67. }
  68.  
  69. template <class T>
  70. Varray<T>::~Varray ()
  71. {
  72.     delete m_array;
  73. }
  74.  
  75. template <class T>
  76. Ref <T> Varray<T>::operator [] (const Position position)
  77. {
  78.     return retrieve_element_at (position);
  79. }
  80.  
  81. template <class T>
  82. void Varray<T>::remove_element_at (const Position position)
  83. {
  84.     assert (position >= 0);
  85.     assert (position < cardinality ());
  86.  
  87.     for (Position i = position; i < m_count - 1; i++)
  88.     m_array [i] = m_array [i + 1];
  89.  
  90.     m_array [--m_count] = 0;
  91.  
  92.     if (m_total - m_count > m_chunk * 2)
  93.     resize (m_total - m_chunk * 2);
  94. }
  95.  
  96. template <class T>
  97. void Varray<T>::replace_element_at (Ref <T> element, const Position position)
  98. {
  99.     assert (position >= 0);
  100.     assert (position < cardinality ());
  101.     m_array [position] = element;
  102. }
  103.  
  104. template <class T>
  105. Ref <T> Varray<T>::retrieve_element_at (const Position position)
  106. {
  107.     assert (position >= 0);
  108.     assert (position < cardinality ());
  109.     return m_array [position];
  110. }
  111.  
  112. template <class T>
  113. void Varray<T>::resize (Position length)
  114. {
  115.     if (length) {
  116.     Ref <T> *array = new Ref <T> [length];
  117.     for (int i = 0; i < length &&i < m_total; i++)
  118.         array [i] = m_array [i];
  119.     while (i < length)
  120.         array [i++] = NULL;
  121.     delete m_array;
  122.     m_array = array;
  123.  
  124.     if (length < m_count)
  125.         m_count = length;
  126.     } else {
  127.     delete m_array;
  128.     m_array = NULL;
  129.     m_count = 0;
  130.     }
  131.     m_total = length;
  132. }
  133.  
  134. template <class T>
  135. int Varray<T>::find_element (const Ref <T> ref) const
  136. {
  137.     for (int i = 0; i < m_count; i++)
  138.     if (ref == m_array [i])
  139.         return i;
  140.     return -1;
  141. }
  142.  
  143. template <class T>
  144. int Varray<T>::upper_bound () const
  145. {
  146.     return m_total;
  147. }
  148.  
  149. //
  150. // Collection methods
  151. //
  152.  
  153. template <class T>
  154. void Varray<T>::insert_element (Ref <T> ref)
  155. {
  156.     if (m_count == m_total)
  157.     resize (m_total + m_chunk);
  158.  
  159.     m_array [m_count++] = ref;
  160. }
  161.  
  162. template <class T>
  163. void Varray<T>::remove_element (Ref <T> ref)
  164. {
  165.     remove_element_at (find_element (ref));
  166. }
  167.  
  168. template <class T>
  169. void Varray<T>::replace_element_at (Ref <T> ref, const Iterator <T> &iter)
  170. {
  171.     m_array [iter.get_position ()] = ref;
  172. }
  173.  
  174. template <class T>
  175. Varray<T>::Varray (const Ref <Varray <T> > array) :
  176.     m_chunk (array->m_chunk),
  177.     m_total (0),
  178.     m_array (NULL)
  179. {
  180.     m_is_ordered = array->ordered ();
  181.     m_allows_duplicates = array->allows_duplicates ();
  182. }
  183.  
  184. template <class T>
  185. Ref <Collection <T> > Varray <T>::create (void) const
  186. {
  187.     return Ref <Collection <T> > (new Varray <T> (this));
  188. }
  189.  
  190. #endif
  191.